package cn.xn.hrms.domain.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import cn.xn.hrms.api.dto.res.UserDTO;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.xn.hrms.api.dto.res.LeaveDTO;
import cn.xn.hrms.api.dto.res.PersonalDTO;
import cn.xn.hrms.domain.service.LeaveService;
import cn.xn.hrms.domain.utils.FunctionUtils;
import cn.xn.hrms.infrastructure.dao.LeaveDao;
import cn.xn.hrms.infrastructure.dao.PersonalDao;
import lombok.extern.slf4j.Slf4j;

/**
 * @author: ysz
 * @date: 2023/07/21
 **/
@Service
@Slf4j
public class LeaveServiceImpl implements LeaveService {

    @Resource
    private LeaveDao leaveDao;

    @Resource
    private PersonalDao personalDao;

    @Override
    public void create(LeaveDTO leaveDTO) {
        log.info("创建请假申请，入参：{}", JSON.toJSONString(leaveDTO));
        // 默认已提交
        leaveDTO.setLeaveStatus(1);
        leaveDTO.setLeaveIsDeleted(0);
        leaveDao.insertSelective(leaveDTO);

    }

    @Override
    public PageInfo<LeaveDTO> page(Integer page, Integer size, Integer leaver, Integer type, Integer status) {

        log.info("分页查询请假单，入参：{},{},{}", leaver, type, status);
        PageHelper.startPage(page, size);
        List<LeaveDTO> leaveList = leaveDao.findAllByParams(status, type, leaver);
        FunctionUtils.isTrueOrFalse(!CollectionUtils.isEmpty(leaveList)).trueOrFalseHandle(() -> {
            List<Integer> leaverList = leaveList.stream().map(LeaveDTO::getPersonalId).collect(Collectors.toList());
            List<PersonalDTO> personalDTOList = personalDao.findAllByIdIn(leaverList);
            FunctionUtils.isTrueOrFalse(!CollectionUtils.isEmpty(personalDTOList)).trueOrFalseHandle(()->{
                Map<Integer, String> personalMap =
                        personalDTOList.stream().collect(Collectors.toMap(PersonalDTO::getPersonalId, PersonalDTO::getName,
                        (v1, v2) -> v2));

                for (LeaveDTO leaveDTO : leaveList) {
                    leaveDTO.setLeaverName(personalMap.get(leaveDTO.getPersonalId()));
                }

            },()->{});
        }, () -> {

        });
        return new PageInfo<>(leaveList);
    }

    @Override
    public void delete(List<Long> ids) {
        log.info("删除请假单:ids={}", ids);
        leaveDao.updateIsDeletedByIdIn(1, ids);
    }

    @Override
    public void update(LeaveDTO leaveDTO) {
        log.info("修改请假单，{}", JSON.toJSONString(leaveDTO));
        LeaveDTO findLeave = leaveDao.selectByPrimaryKey(leaveDTO.getLeaveId());
        leaveDTO.setPersonalIdApprove(findLeave.getPersonalIdApprove());
        leaveDTO.setApproveTime(findLeave.getApproveTime());
        leaveDTO.setLeaveStatus(findLeave.getLeaveStatus());
        leaveDao.updateByPrimaryKey(leaveDTO);
    }

    @Override
    public void approve(Long id, Integer status, String remark) {
        log.info("审批请假单，id:{}", id);
        // 获取登录用户
        UserDTO userDTO = (UserDTO) SecurityUtils.getSubject().getPrincipal();
        log.error(JSON.toJSONString(userDTO));
        leaveDao.updateStatusAndApproverAndApproveTimeById(status, userDTO.getUserId(), new Date(), id, remark);

    }

    @Override
    public void batchApprove(List<Long> ids, Integer status) {
        log.info("批量审批请假单，{}", ids);
        UserDTO userDTO = (UserDTO) SecurityUtils.getSubject().getPrincipal();
        leaveDao.updateStatusAndApproverAndApproveTimeByIdIn(status, userDTO.getUserId(), new Date(), ids);

    }

    @Override
    public List<LeaveDTO> listAll(Integer leaveStatus) {

        log.info("查询所有的请假单");
        return leaveDao.listAll(leaveStatus);

    }
}
